<template>
  <div>
    <el-card shadow="never" class="ivu-mt" :body-style="{padding:0}">
      <div class="padding-add">
        <el-form
            ref="curlFrom"
            :model="from"
            :label-width="labelWidth"
            :label-position="labelPosition"
            inline
            @submit.native.prevent
        >
          <el-form-item label="标题：" label-for="title">
            <el-input
                v-model="from.title"
                placeholder="请输入标题"
                class="form_content_width"
            />
          </el-form-item>

          <el-form-item label="发帖人：" label-for="user_id">
            <el-input
                v-model="from.user_id"
                placeholder="请输入发帖人"
                class="form_content_width"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="searchs">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card shadow="never" dis-hover class="ivu-mt mt16">
      <el-row type="flex">
        <el-col v-bind="grid">
          <el-button v-auth="['posts-add']" type="primary" icon="md-add" @click="add">添加</el-button>
        </el-col>
      </el-row>
      <el-table
          :data="dataList"
          ref="table"
          class="mt25"
          :loading="loading"
          highlight-current-row
      >
        <el-table-column prop="title" label="标题">
        </el-table-column>
<!--        <el-table-column prop="content" label="内容">-->
<!--        </el-table-column>-->
        <el-table-column prop="user_name" label="发帖人">
        </el-table-column>
        <el-table-column prop="universit_name" label="所在院校">
        </el-table-column>
        <el-table-column prop="like" label="点赞">
        </el-table-column>
        <el-table-column prop="browse" label="观看人数">
        </el-table-column>
        <el-table-column prop="forward" label="转发数">
        </el-table-column>
<!--        <el-table-column prop="universi_type_id" label="帖子分类">-->
<!--        </el-table-column>-->
<!--        <el-table-column label="图片">-->
<!--          <template slot-scope="scope">-->
<!--            <div class="tabBox_img" v-viewer>-->
<!--              <img v-lazy="img" v-for="img in scope.row.images_label" />-->
<!--            </div>-->
<!--          </template>-->
<!--        </el-table-column>-->

        <el-table-column prop="status_label" label="帖子状态">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <a @click="show(scope.row)">详情</a>
            <el-divider direction="vertical" />
            <a @click="edit(scope.row)">修改</a>
            <el-divider direction="vertical" />
            <a @click="del(scope.row, '删除', scope.$index)">删除</a>
          </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
        <pagination :total="total"  @pagination="pageChange"  :limit.sync="from.limit" :page.sync="from.page" />
      </div>
    </el-card>

    <el-dialog title="查看详情" :visible.sync="dialogTableVisible" v-if='dialogTableVisible'>
      <el-descriptions title="posts">
        <el-descriptions-item label="自增ID">{{info.id}}</el-descriptions-item>
        <el-descriptions-item label="标题">{{info.title}}</el-descriptions-item>
        <el-descriptions-item label="内容">{{info.content}}</el-descriptions-item>
        <el-descriptions-item label="发帖人">{{info.user_id}}</el-descriptions-item>
        <el-descriptions-item label="所在院校">{{info.universit_id}}</el-descriptions-item>
        <el-descriptions-item label="点赞">{{info.like}}</el-descriptions-item>
        <el-descriptions-item label="评论">{{info.browse}}</el-descriptions-item>
        <el-descriptions-item label="转发数">{{info.forward}}</el-descriptions-item>
<!--        <el-descriptions-item label="帖子分类">{{info.universi_type_id}}</el-descriptions-item>-->
        <el-descriptions-item label="图片"><el-image v-for="item in info.images_label" :src="item" :preview-src-list="info.images_label">
        </el-image>
        </el-descriptions-item>
        <el-descriptions-item label="帖子状态">{{info.status_label}}</el-descriptions-item>
<!--        <el-descriptions-item label="添加时间">{{info.create_time}}</el-descriptions-item>-->
<!--        <el-descriptions-item label="修改时间">{{info.update_time}}</el-descriptions-item>-->
<!--        <el-descriptions-item label="伪删除">{{info.delete_time}}</el-descriptions-item>-->

      </el-descriptions>
    </el-dialog>

    <el-dialog title="帖子" :visible.sync="dialogFormVisible">
      <posts-add @postsAdd="postsAddData" ref="postsAddForm"></posts-add>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import {
  postsSaveApi,
  postsStatusApi,
  postsDeleteApi,
  postsUpdateApi,
  getPostsCreateApi,
  getPostsEditApi,
  getPostsListApi,
  getPostsReadApi,
  postsSaveApiV2
} from '@/api/crud/posts';
import PostsAdd from "@/pages/socialize/posts/components/postsAdd.vue";
export default {
  name: 'posts',
  components: {PostsAdd},
  data() {
    return {
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      loading: false,
      from: {
        title:'',
        user_id:'',
        page: 1,
        limit: 15,
      },
      dataList: [],
      total: 0,
      dialogTableVisible: false,
      info: {},
      dialogFormVisible: false,
      postsForm: {

      }
    };
  },

  computed: {
    ...mapState('media', ['isMobile']),
    labelWidth() {
      return this.isMobile ? undefined : '75px';
    },
    labelPosition() {
      return this.isMobile ? 'top' : 'left';
    },
  },
  created() {
    this.getList();
  },
  methods: {
    postsAddData(data) {
      console.log(data)
      if (data.id != null && data.id != 0){
        postsUpdateApi(data.id, data).then((res) => {
          if (res.status == 200) {
            this.dialogFormVisible = false
            this.$message.success("修改成功")
            this.getList()
          }
          else {
            this.$message.error("修改失败")
          }
        })
      }
      else {
        postsSaveApiV2(data).then((res) => {
          if (res.status == 200) {
            this.dialogFormVisible = false
            this.$message.success("保存成功")
            this.getList()
          }
          else {
            this.$message.error("保存失败")
          }
        })
      }
    },
    show(row) {
      getPostsReadApi(row.id).then(res => {
        this.dialogTableVisible = true;
        this.info = res.data;
      }).catch(res => {
        this.$Message.error(res.msg);
      })
    },
    //修改状态
    updateStatus(row, field) {
      postsStatusApi(row.id, {field: field, value: row[field]})
          .then(async (res) => {
            this.$message.success(res.msg);
          })
          .catch((res) => {
            this.$message.error(res.msg);
          });
    },
    // 添加
    add() {
      // this.$modalForm(getPostsCreateApi()).then(() => this.getList());
      this.dialogFormVisible = true
      this.$refs.postsAddForm.initData({}, 'add')
    },
    // 表格搜索
    searchs() {
      this.from.page = 1;
      this.getList();
    },
    //列表
    getList() {
      this.loading = true;
      getPostsListApi(this.from)
          .then(async (res) => {
            let data = res.data;
            this.dataList = data.list;
            this.total = data.count;
            this.loading = false;
          })
          .catch((res) => {
            this.loading = false;
            this.$Message.error(res.msg);
          });
    },
    //分页
    pageChange(index) {
      this.from.page = index;
      this.getList();
    },
    // 修改
    edit(row) {
      this.dialogFormVisible = true
      this.$refs.postsAddForm.initData(row, 'edit')
    },
    // 删除
    del(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        url: `crud/posts/${row.id}`,
        method: 'DELETE',
        ids: '',
      };
      this.$modalSure(delfromData)
          .then((res) => {
            this.$Message.success(res.msg);
            this.getList();
          })
          .catch((res) => {
            this.$Message.error(res.msg);
          });
    },
  },
};
</script>

<style scoped lang="stylus"></style>
